//
//  CategoryListTabView.swift
//  IdleFish
//
//  Created by PTZM_IMAC_12 on 2021/9/23.
//

import SwiftUI

struct CategoryListTabView<ActionView: View>: View {
    
    var categoryList: [HomeRecommendGoodsCategoryModel]
    @Binding var currCategoryTab: HomeRecommendGoodsCategoryModel?
    @Binding var currCategoryTabIndex: Int
    
    
    var actionView: () -> ActionView
    var actionViewWidth: CGFloat
    
    init(categoryList: [HomeRecommendGoodsCategoryModel], currCategoryTab: Binding<HomeRecommendGoodsCategoryModel?>, currCategoryTabIndex: Binding<Int>, actionViewWidth: CGFloat, @ViewBuilder actionView: @escaping () -> ActionView) {
        
        self.categoryList = categoryList
        self._currCategoryTab = currCategoryTab
        self._currCategoryTabIndex = currCategoryTabIndex
        self.actionView = actionView
        self.actionViewWidth = actionViewWidth
    }
    
    @Namespace var animation
    
    var body: some View {
        
        ScrollView(.horizontal, showsIndicators: false) {
            
            LazyHStack {
                ForEach(categoryList) { tab in
                    HomeRecommendGoodsCategoryTabBtn(tab: tab, selectedTab: currCategoryTab, animation: animation, selectCategoryTabBlock: { tab in
                        
                        currCategoryTab = tab
                        withAnimation(.spring()) {
                            
                            let selectedTabIndex = categoryList.firstIndex(of: tab)!
                            currCategoryTabIndex = selectedTabIndex
                        }
                    })
                }
                
                if actionViewWidth != 0 {
                    Color.clear
                        .frame(width: actionViewWidth)
                }
                
            }
            .padding(.leading, 16)
            .padding(.vertical, 8)
        }
        .overlay(
            
            actionView()
                
            ,alignment: .trailing
        )
        
    }
}
